package com.qding.park.manager.app.queue.handle;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qding.park.common.core.constant.MongodbConstant;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.manager.app.queue.HandleWrap;
import com.qding.park.manager.pojo.queue.QueueMsg;

/**
 * @author Administrator
 *失败任务重做
 */
@Component("failTaskRedoJobHandle")
public class FailTaskRedoJobHandle extends AbstractAllHandle {

	@Autowired
	private IExceptionHandle exceptionHandle;
	
	@Override
	void actualHandle(QueueMsg msg) throws BaseException {

		// 表名
		String tableName = MongodbConstant.TABLENAME_QUEUEMSG_FAIL;

		// 读取所有的失败任务
		List<QueueMsg> msgList = mongoTemplate.findAll(QueueMsg.class, tableName);

		for (QueueMsg tempmsg : msgList) {

			try {
				String jsonData = JSON.toJSONString(tempmsg.getData());
				tempmsg.setSerail(0);
//				tempmsg.setRetryNum(tempmsg.getRetryNum()+1);
				JSONObject jsonObject = JSON.parseObject(jsonData);
				tempmsg.setData(jsonObject);
				HandleWrap.handle(tempmsg);
				mongoTemplate.remove(tempmsg, tableName);
			} catch (Exception e) {
				exceptionHandle.handle(e);
			}
		}

		// HandleWrap.handle(msg);
	}

	@Override
	boolean failDiscardRollBacK(QueueMsg msg) {
		_log.error("失败任务重做回滚:"+msg);
		return true;
	}

}
